'''
https://leetcode.cn/problems/binary-tree-paths/
'''

from typing import Optional, List


class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
class Solution:
    def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:
        res = []
        path = []
        def f(root):
            if root.left is None and root.right is None:
                res.append('->'.join(path + [str(root.val)]))
                return
            path.append(str(root.val))
            if root.left:
                f(root.left)
            if root.right:
                f(root.right)
            path.pop()
        f(root)
        return res